<template>
    <div class="">
        <el-dialog
                :title="title"
                :visible.sync="dialogFormVisible"
                width="600px"
                :close-on-click-modal="false"
                @close="close"
        >
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="桌位类型" prop="tableId" :required="true">
                    <el-select v-model="form.tableId" placeholder="请选择">
                        <el-option
                                v-for="item in typeOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="medium" @click="close">取 消</el-button>
                <el-button size="medium" type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {saveCode,getTable,getSaveCode} from "@/api/instore";

    export default {
        name: "meteriaSave",
        created() {
        },
        data() {
            return {
                form: {
                    id: '',
                    tableId: '',
                },
                typeOptions:[],
                title: "",
                dialogFormVisible: false,
            }
        },
        components: {},
        methods: {
            showEdit(row) {
                if (!row) {
                    this.title = "编辑";
                } else {
                    this.title = "编辑";
                    this.getTable()
                    this.form.id = row.id
                    this.form.type = 1
                    this.getSaveCode(row.id)
                }
                this.dialogFormVisible = true;
            },
            setStoreId(storeId){
                this.form.storeId = storeId
            },
            async getTable() {
                const {data} = await getTable({storeId:this.form.storeId});
                this.typeOptions = data?data:[];
            },
            async getSaveCode(id){
                const {data} = await getSaveCode({id,});
                this.form.tableId = data.data.tableId?data.data.tableId:'';
            },
            close() {
                this.$refs["form"].resetFields();
                this.form = this.$options.data().form;
                this.dialogFormVisible = false;
            },
            save() {
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        const {msg} = await saveCode(this.form);
                        this.$baseMessage(msg, "success");
                        this.$emit("fetchData");
                        this.close();
                    } else {
                        return false;
                    }
                });
            },
        }
    };
</script>
